/**
 * Created by JetBrains PhpStorm.
 * User: maplefalling
 * Date: 1/18/12
 * Time: 9:28 PM
 * To change this template use File | Settings | File Templates.
 */
Ext.ns('AAWE.App.List.Store');

Ext.define('AAWE.App.List.Store.CheckListItemStore',{
    extend:'Ext.data.Store',
    storeId : 'AAWE.App.List.Store.CheckListItemStore',
    autoload:true,
    autoSave:true,
    listeners:{
        datachanged:function(){
            this.save();
        }
    },
    getItems: function (listId) {
        var _items = [];
        for (i = 0; i < this.data.length; i++) {
            _data = this.data.items[i].data;
            if (_data.listId == listId) {
                _items.push(_data);
            }
        }
        return _items;
    },
    getCount: function (listId) {
        var _count = 0, i;
        if (!listId) {
            return -1;
        }
        for (i = 0; i < this.data.length; i++) {
            if (this.data.items[i].data.listId == listId) {
                _count++;
            }
        }
        return _count;
    },
    getCheckedCount: function (listId) {
        var _count = 0, i;
        if (!listId) {
            return -1;
        }
        for (i = 0; i < this.data.length; i++) {
            if (this.data.items[i].data.listId == listId && this.data.items[i].data.isChecked) {
                _count++;
            }
        }
        return _count;
    },
    allChecked: function (listId) {
        var i;

        for (i = 0; i < this.data.length; i++) {
            if (this.data.items[i].data.listId == listId) {
                if (!this.data.items[i].data.isChecked) {
                    return false;
                }
            }
        }
        return true;
    },
    check: function (id, checked) {
        var _index = this.findExact('id', id);
        var _r;
        if (_index != -1) {
            _r = this.getAt(_index);
            _r.data.isChecked = checked;
            _r.setDirty();
            _r.save();
        }
        this.sync();
    },
    deleteItem: function (id) {
        var _index = this.findExact('id', id);
        if (_index != -1) {
            this.removeAt(_index);
            this.sync();
        }
    },
    editName: function (id, name) {
        var _index = this.findExact('id', id);
        var _r;
        if (_index != -1) {
            _r = this.getAt(_index);
            _r.data.itemname = name;
            _r.setDirty();
            _r.save();
        }
        this.sync();
    }
});